//
//  YLMonthlyBillViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/26.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMonthlyBillViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import <YYModel.h>
#import "YLHoldingAssetsModel.h"
@interface YLMonthlyBillViewModel ()

/**
 持有资产
 */
@property (nonatomic, strong) YLHoldingAssetsItem *holdingAssetsItem;

@property (nonatomic, assign) BOOL holdingAssetsTag;

/**
 到账收益
 */
@property (nonatomic, strong) YLHoldingAssetsItem *returnOnAccountItem;

@property (nonatomic, assign) BOOL returnOnAccountTag;

/**
 预期收益
 */
@property (nonatomic, strong) YLHoldingAssetsItem *expectedReturnItem;

@property (nonatomic, assign) BOOL expectedReturnTag;

@end

@implementation YLMonthlyBillViewModel

// 到账收益
- (RACCommand *)returnOnAccountCommand
{
    
    if (!_returnOnAccountCommand) {
        __weak typeof(self) weakSelf = self;
        _returnOnAccountCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            weakSelf.returnOnAccountItem = nil;
            weakSelf.returnOnAccountTag = NO;
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                NSDictionary *param = @{@"TimeIndex" : input};
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PCumulativeAmountByMonth", YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                
                        weakSelf.returnOnAccountItem = [YLHoldingAssetsItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    weakSelf.returnOnAccountTag = YES;
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                     [subscriber sendCompleted];
                    weakSelf.returnOnAccountTag = YES;
                }];
            
                return nil;
            }];
            
        }];
    }
    return _returnOnAccountCommand;
}


// 持有资产
- (RACCommand *)holdingAssetsCommand
{
    __weak typeof(self) weakSelf = self;
    if (!_holdingAssetsCommand) {
        _holdingAssetsCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            weakSelf.holdingAssetsItem = nil;
            weakSelf.holdingAssetsTag = NO;
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [WHProgressHUD showStatus:@"正在切换至持有资产"];
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetInvestmentAmountByMonth", YLURL];
                
                NSDictionary *param = @{@"TimeIndex" : input};
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                       
                        weakSelf.holdingAssetsItem = [YLHoldingAssetsItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    weakSelf.holdingAssetsTag = YES;
                    [subscriber sendCompleted];
                    
                    
                } fail:^(NSError *error) {
                     weakSelf.holdingAssetsTag = YES;
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _holdingAssetsCommand;
}

// 预期收益
- (RACCommand *)expectedReturnCommand
{
    if (!_expectedReturnCommand) {
        __weak typeof(self) weakSelf = self;
        _expectedReturnCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            weakSelf.expectedReturnItem = nil;
            weakSelf.expectedReturnTag = NO;
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [[WHNetworkTool shareInstance] cancel];
                [WHProgressHUD showStatus:@"正在切换至预期收益"];
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCumulativeAmountListByMonth", YLURL];
                
                NSDictionary *param = @{@"TimeIndex" : input};
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        weakSelf.expectedReturnItem = [YLHoldingAssetsItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    // 请求结束后就标记为请求结束
                    weakSelf.expectedReturnTag = YES;
                    [subscriber sendCompleted];
                    
                    
                } fail:^(NSError *error) {
                    weakSelf.expectedReturnTag = YES;
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    return _expectedReturnCommand;
}

- (instancetype)init
{
    if (self = [super init]) {
        _holdingAssetsTag = _expectedReturnTag = _returnOnAccountTag = NO;
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    // 监听是否有改变
    _holdingAssetsSignal = [RACSignal combineLatest:@[RACObserve(self, holdingAssetsTag), RACObserve(self, returnOnAccountTag)] reduce:^id _Nullable(NSNumber *holdingAssetsTag, NSNumber  *returnOnAccountTag){
        
        // 如果都为yes 说明全部请求完
        if ([holdingAssetsTag boolValue] && [returnOnAccountTag boolValue]) {
            [WHProgressHUD dismiss];
            // 请求时把数组的内容去了，防止出错
             [weakSelf.group removeAllObjects];
             weakSelf.holdingAssetsItem.title = @"持有资产详情";
            if (weakSelf.holdingAssetsItem) {
                 [weakSelf.group addObject:weakSelf.holdingAssetsItem];
            }
            

                weakSelf.returnOnAccountItem.title = @"到账收益";
            if (weakSelf.returnOnAccountItem) {
                [weakSelf.group addObject:weakSelf.returnOnAccountItem];
            }

            weakSelf.holdingAssetsTag = weakSelf.returnOnAccountTag = weakSelf.expectedReturnTag = NO;
        }
    
        return @([holdingAssetsTag boolValue] && [returnOnAccountTag boolValue]);
    }];
    
    _expectedReturnSignal = [RACSignal combineLatest:@[RACObserve(self, expectedReturnTag),RACObserve(self, returnOnAccountTag)] reduce:^id _Nullable(NSNumber *expectedReturnTag, NSNumber  *returnOnAccountTag){
        
        // 如果都为yes 说明全部请求完
        if ([expectedReturnTag boolValue] && [returnOnAccountTag boolValue]) {
            [WHProgressHUD dismiss];
            // 请求时把数组的内容去了，防止出错
            [weakSelf.group removeAllObjects];
            
//            if (weakSelf.expectedReturnItem) {
//                if (weakSelf.expectedReturnItem.item.count > 0) {
                    weakSelf.expectedReturnItem.title = @"预期收益详情";
            if (weakSelf.expectedReturnItem) {
                [weakSelf.group addObject:weakSelf.expectedReturnItem];
            }
            

                    weakSelf.returnOnAccountItem.title = @"到账收益";
            if (weakSelf.returnOnAccountItem) {
                [weakSelf.group addObject:weakSelf.returnOnAccountItem];

            }

            weakSelf.holdingAssetsTag = weakSelf.returnOnAccountTag = weakSelf.expectedReturnTag = NO;
        }
        
        return @([expectedReturnTag boolValue] && [returnOnAccountTag boolValue]);
        
    }];
    
}

- (NSMutableArray *)group
{
    if (!_group) {
        _group = [NSMutableArray array];
    }
    return _group;
}

- (void)dealloc
{
    [WHProgressHUD dismiss];
    [[WHNetworkTool shareInstance] cancel];
}

@end
